package com.ccp.dev.system.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CollectionUtils
 * @Description: 集合工具类
 * @author luozhifeng
 * @date 2014-12-10 上午11:25:31
 */
public class CollectionUtils {
    public static <T> Map<Integer,T> listToMap(List<T> list, Class<T> clazz){
        Map<Integer, T> map = new HashMap<Integer, T>();
        for(T t : list){
            try {
                Method method1 = clazz.getMethod("getId");
                Object o = method1.invoke(t);
                map.put((Integer) o, t);
            }catch(Exception e){
            }
        }
        return map;
    }

    public static <T> Map<String,T> listToStrMap(List<T> list, Class<T> clazz, String property){
        Map<String, T> map = new HashMap<String, T>();
        for(T t : list){
            try {
                Method method1 = clazz.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
                Object o = method1.invoke(t);
                map.put((String) o, t);
            }catch(Exception e){
            }
        }
        return map;
    }

    public static <T> List<Object> getPropertys(List<T> list, Class<T> clazz, String property) {
        List<Object> results = new ArrayList<Object>();
        if (list == null) {
            return results;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                T t = list.get(i);
                Method method1 = clazz.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
                Object o = method1.invoke(t);
                if(o!=null){
                    results.add(o);
                }
            }
        } catch (Exception e) {
            System.out.println("转换异常");
        }
        return results;
    }


    public static <T> List<String> getStringPropertys(List<T> list, Class<T> clazz, String property) {
        List<String> results = new ArrayList<String>();
        if (list == null) {
            return results;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                T t = list.get(i);
                Method method1 = clazz.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
                Object o = method1.invoke(t);
                results.add((String) o);
            }
        } catch (Exception e) {
            System.out.println("转换异常");
        }
        return results;
    }

    public static <T> List<Integer> getIds(List<T> list, Class<T> clazz, String property) {
        List<Integer> results = new ArrayList<Integer>();
        if (list == null) {
            return results;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                T t = list.get(i);
                Method method1 = clazz.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
                Object o = method1.invoke(t);
                results.add((Integer) o);
            }
        } catch (Exception e) {
            System.out.println("转换异常");
        }
        return results;
    }

    public static <T> List<Integer> getIds(List<T> list, Class<T> clazz) {
        List<Integer> results = new ArrayList<Integer>();
        if (list == null) {
            return results;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                T t = list.get(i);
                Method method1 = clazz.getMethod("getId");
                Object o = method1.invoke(t);
                results.add((Integer) o);
            }
        } catch (Exception e) {
            System.out.println("转换异常");
        }

        return results;
    }

    /**
     * @Description: 按照ids 排序 ids为 实体的id
     * @param list
     * @param ids
     * @return
     * @author luozhifeng
     * @date 2014-12-10 上午11:28:56
     * @version V1.0
     */
    public static <T> List<T> sortList(List<T> list, List<Integer> ids,
                                       Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (list == null || ids == null) {
            return result;
        }
        try {
            for (int i = 0; i < ids.size(); i++) {
                for (T t : list) {
                    Method method1 = clazz.getMethod("getId");
                    Object o = method1.invoke(t);
                    Integer id = (Integer) o;
                    if (id.intValue() == ids.get(i).intValue()) {
                        result.add(t);
                        list.remove(t);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("转换异常");
            return result;
        }
        return result;
    }


}
